package com.example.servlet;

import com.example.dao.LoginDao;
import com.example.pojo.Member;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/doLogin")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       this.doPost(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String user= req.getParameter("user");
        String pass=req.getParameter("pass");
        String group=req.getParameter("group");
        PrintWriter out = resp.getWriter();
        /*out.println(user+"->"+pass+"->"+group);
        out.println("我相信拦截器起作用了,所以中文不会乱码");*/
        //登录成功则转发
        //登录失败重定向
        LoginDao dao=new LoginDao();
        switch (group)
        {
            case "user1":
                //普通会员
                if(dao.memberLogin(user, pass)!=null)
                {
                    //out.println(user+pass);
                    req.getRequestDispatcher("member.jsp").forward(req,resp);
                }
                else
                    resp.sendRedirect("index.jsp");
                break;
            case "user2":
                //教练员
                if(dao.coachLogin(user,pass)!=null)
                {
                    req.getRequestDispatcher("coach.jsp").forward(req,resp);
                }
                else
                    resp.sendRedirect("index.jsp");
                break;
            case "user3":
                //管理员
                if(dao.adminLogin(user, pass))
                {
                    //out.println(user+pass);
                    //req.getRequestDispatcher("admin_page/admin.jsp").forward(req,resp);
                    resp.sendRedirect("admin_page/admin.jsp");
                }
                else
                    resp.sendRedirect("index.jsp");
                break;
            default:
                break;
        }


    }

}
